<?php

namespace App\Http\Controllers\Api;

use App\Category;
use App\Http\Requests\StoreCategory;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class CategoryController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $categories = Category::orderBy('id', 'desc');
        if ($request->get('belongs_to')) {
            $categories->where('belongs_to', $request->get('belongs_to'));
        }
        $categories = $categories->get();
        if ($request->get('data_type') == 'tree') {
            $categories = Category::tree($categories);
        } else {
            $categories = Category::treeData($categories);
        }
        return $categories;
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(StoreCategory $request)
    {
        $data = $request->all();
        if ($request->get('parent_id')) {
            $parent = Category::findOrFail($request->get('parent_id'));
            $data['depth'] = $parent->depth + 1;
        }
        Category::create($data);
        return response(['message' => '创建成功']);
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Category  $category
     * @return \Illuminate\Http\Response
     */
    public function show(Category $category)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Category  $category
     * @return \Illuminate\Http\Response
     */
    public function edit(Category $category)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Category  $category
     * @return \Illuminate\Http\Response
     */
    public function update(StoreCategory $request, Category $category)
    {
        if ($request->parent_id == $category->id) {
            return response(['message' => '父级分类不能为自己'], 400);
        }
        if ($category->update($request->only(['name', 'parent_id', 'order', 'keywords', 'description', 'is_show', 'tpl', 'belongs_to']))) {
            return response(['message' => '修改成功']);
        }
        return response(['message' => '修改失败'], 400);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Category  $category
     * @return \Illuminate\Http\Response
     */
    public function destroy(Category $category)
    {
        if (count($category->children) > 0) {
            return response(['message' => '该分类下存在子分类，不能删除！'], 400);
        }
        if (count($category->goods) > 0) {
            return response(['message' => '该分类下存在商品，不能删除！'], 400);
        }
        if ($category->delete()) {
            return response(['message' => '删除成功']);
        }
        return response(['message' => '删除失败'], 400);
    }
}
